package com.rabbitmq.produce;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.AsyncRabbitTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.rabbitmq.produce.callback.ConfirmCallbackConfig.messageMap;

/**
 * @author zhangbowen
 * @date 2019/3/11
 **/
@RestController
public class Produce {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private AsyncRabbitTemplate asyncRabbitTemplate;


    @GetMapping("publish1")
    public void publish1() {
        MessageProperties properties = MessagePropertiesBuilder.newInstance().build();
        properties.setExpiration("6000");
        properties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        properties.setContentType(MediaType.APPLICATION_JSON_VALUE);
        int i = 0;
        Message message = MessageBuilder.withBody(("我是test1" + i).getBytes()).andProperties(properties).build();
        rabbitTemplate.setRetryTemplate(new RetryTemplate());
        rabbitTemplate.send("topicExchange", "topic.test.1", message);
        rabbitTemplate.send("topicExchange", "topic.test.1", message);
        rabbitTemplate.send("topicExchange", "topic.test.1", message);
        rabbitTemplate.send("topicExchange", "topic.test.1", message);
        rabbitTemplate.send("topicExchange", "topic.test.1", message);
        rabbitTemplate.send("topicExchange", "topic.test.1", message);
        rabbitTemplate.send("topicExchange", "topic.test.1", message);
    }

    public void publish2() {
        String messageId = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData(messageId);
        Message message = MessageBuilder.withBody("我是test2".getBytes())
                .setContentType("application/json").setMessageId(messageId).build();
        //rbbitTemplate.setReplyTimeout(4000);
        rabbitTemplate.convertAndSend("topicExchange", "topic.test.2.1", message, correlationData);
        Map<String, Object> map = new HashMap<>();
        map.put("exchange", "topicExchange");
        map.put("routingKey", "topic.test.2");
        map.put("message", message);
        messageMap.put(messageId, map);
    }

    @GetMapping(value = "asyncPublish")
    public void asyncPublish() {
        Test test = new Test();
        test.setA("生产者1");
        test.setAge(20);
        ListenableFuture<JSONObject> future = asyncRabbitTemplate.convertSendAndReceive("topicExchange", "topic.test.1", test);
        future.addCallback(new ListenableFutureCallback<JSONObject>() {
            @Override
            public void onFailure(Throwable ex) {
                System.out.println(JSONObject.toJSONString(ex));
            }

            @Override
            public void onSuccess(JSONObject result) {
                System.out.println(result.toJSONString());
            }
        });
    }
}

@Data
class Test implements Serializable {
    private static final long serialVersionUID = 4729304244229542768L;
    private String a = "哈哈";
    private int age = 18;
}
